package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IStaffService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 员工管理Controller
 * 
 * @author ruoyi
 * @date 2024-12-11
 */
@RestController
@RequestMapping("/system/staff")
public class StaffController extends BaseController
{
    @Autowired
    private IStaffService staffService;

    /**
     * 查询员工管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:staff:list')")
    @GetMapping("/list")
    public TableDataInfo list(Staff staff)
    {
        startPage();
        List<Staff> list = staffService.selectStaffList(staff);
        startPage();
        List<Long> num = staffService.selectCount();
        for(int i = 0; i < list.size(); i++){
            list.get(i).setStaffNum(num.get(i));
        }
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:staff:list')")
    @GetMapping("/propertyList")
    public AjaxResult propertyList()
    {

        return success(staffService.selectPropertyList());
    }

    @PreAuthorize("@ss.hasPermi('system:staff:list')")
    @GetMapping("/timeList")
    public AjaxResult timeList()
    {
        return success(staffService.selectTimeList());
    }


    /**
     * 导出员工管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:staff:export')")
    @Log(title = "员工管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Staff staff)
    {
        List<Staff> list = staffService.selectStaffList(staff);
        ExcelUtil<Staff> util = new ExcelUtil<Staff>(Staff.class);
        util.exportExcel(response, list, "员工管理数据");
    }

    /**
     * 获取员工管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:staff:query')")
    @GetMapping(value = "/{staffId}")
    public AjaxResult getInfo(@PathVariable("staffId") Long staffId)
    {
        return success(staffService.selectStaffByStaffId(staffId));
    }

    /**
     * 新增员工管理
     */
    @PreAuthorize("@ss.hasPermi('system:staff:add')")
    @Log(title = "员工管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Staff staff)
    {
        int index = staffService.insertStaff(staff);
        System.out.println(index);
        return success(index);
    }

    @PreAuthorize("@ss.hasPermi('system:staff:add')")
    @Log(title = "员工管理", businessType = BusinessType.INSERT)
    @PostMapping("/addProperty")
    public AjaxResult addProperty(@RequestBody AddProperty ap){
        Long staffId=ap.getStaffId();
        List<Long> propIds=ap.getPropIds();
        List<StaffProperty> list=new ArrayList<>();
        for(Long propId:propIds){
            StaffProperty staffProperty=new StaffProperty();
            staffProperty.setStaffId(staffId);
            staffProperty.setPropId(propId);
            list.add(staffProperty);
        }
        return success(staffService.insertStaffProperty(list));
    }
    @PreAuthorize("@ss.hasPermi('system:staff:add')")
    @Log(title = "员工管理", businessType = BusinessType.INSERT)
    @PostMapping("/addTime")
    public AjaxResult addTime(@RequestBody AddTime at){
        Long staffId=at.getStaffId();
        List<Long> timeIds=at.getTimeIds();
        List<StaffTime> list=new ArrayList<>();
        for(Long timeId:timeIds){
            StaffTime staffTime=new StaffTime();
            staffTime.setStaffId(staffId);
            staffTime.setTimeId(timeId);
            list.add(staffTime);
        }
        return success(staffService.insertStaffTime(list));
    }
    /**
     * 修改员工管理
     */
    @PreAuthorize("@ss.hasPermi('system:staff:edit')")
    @Log(title = "员工管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Staff staff)
    {
        return toAjax(staffService.updateStaff(staff));
    }

    @PreAuthorize("@ss.hasPermi('system:staff:edit')")
    @Log(title = "员工管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{staffId}")
    public AjaxResult deleteProperty(@PathVariable Long staffId)
    {
        return toAjax(staffService.deleteProperty(staffId));
    }
    @PreAuthorize("@ss.hasPermi('system:staff:edit')")
    @Log(title = "员工管理", businessType = BusinessType.UPDATE)
    @PutMapping("/time/{staffId}")
    public AjaxResult deleteTime(@PathVariable Long staffId)
    {
        return toAjax(staffService.deleteTime(staffId));
    }

    /**
     * 删除员工管理
     */
    @PreAuthorize("@ss.hasPermi('system:staff:remove')")
    @Log(title = "员工管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{staffIds}")
    public AjaxResult remove(@PathVariable Long[] staffIds)
    {
        return toAjax(staffService.deleteStaffByStaffIds(staffIds));
    }
}
